<template>
  <div id="box">

    <div class="login-body">
      <div class="title">xxx商城管理平台</div>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">登陆</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

  </div>
</template>

<script>
//import { mapState, mapMutations, mapActions, mapGetters } from "vuex";
import AX from "./axios"
import Cookies from "js-cookie"

const ax = new AX()
export default {
  name: "login",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else {
        if (this.ruleForm.username !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: '',
        password: '',

      },
      rules: {
        username: [
          {validator: validatePass, trigger: 'blur'}
        ],
        password: [
          {validator: validatePass2, trigger: 'blur'}
        ],
      }
    }


  },
  created() {
  },
  methods: {
    // ...mapMutations("model",[]),
    // ...mapActions("model",[]),
    submitForm() {
      //用户登陆
      ax.getToken({username:this.ruleForm.username, password:this.ruleForm.password}).then(response=>{
          ax.getUserRole({username: response.data.username}).then(response=>{
            let rolesArr = []
            response.data.roles.forEach((item)=>{
              rolesArr.push(item.description)
            })
            Cookies.set("roles", rolesArr.join(","))
            //设置cookie,
            Cookies.set("token", response.data.token, { expires: 0.01 })
            Cookies.set("username", response.data.username, { expires: 0.01 })
            //跳转到首页
            this.$router.push({name:"index"})
            this.$message({
              type:"success",
              message:"登陆成功",
              duration:1000

          })



          })

      }).catch(()=>{
        this.$message({
          type:"error",
          message: "登陆失败,用户名密码错误",
          duration: 1000
        })
      })


    },
    resetForm() {
      this.ruleForm = {}
    }
  },


  computed:{
    // ...mapState("model",["count"]),
    // ...mapGetters("model",["dbcount"])
  },

}
</script>

<style scoped lang="less">
#box{
  overflow: hidden;
  width: 100%;
  height: 100%;
  background: url("../../assets/t01284e8d319fa012c6的副本.jpg") no-repeat;
  .login-body{
    width: 400px;
    margin: 0 auto;
    margin-top: calc(20% - 40px);
    .title{
      width: 250px;
      margin: 0 auto;
      font-size: 30px;
      margin-bottom: 10px;
    }
  }

}

</style>